/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package COMMON;

import COMMON.CauHoi;
import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
/**
 *
 * @author samph
 */
public class NganHangCauHoi {

    private static ArrayList listQuestionEasy = new ArrayList();
    private static ArrayList listQuestionNormal = new ArrayList();
    private static ArrayList listQuestionHard = new ArrayList();
    private static ArrayList dsch = new ArrayList();
    private static int flag = 0;
    
    public static ArrayList docDanhSachCauHoi(String path) {
        
        try {

            File file = new File(path + "questionBank.xml");
            FileInputStream fileInput = new FileInputStream(file);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(file);
            doc.getDocumentElement().normalize();
            System.out.println("Root element " + doc.getDocumentElement().getNodeName());
            NodeList nodeLst = doc.getElementsByTagName("Cauhoi");
            //System.out.println("Information of all employees");

            for (int s = 0; s < nodeLst.getLength(); s++) {
                CauHoi ch = new CauHoi();
                Node fstNode = nodeLst.item(s);

                if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element Elmnt = (Element) fstNode;


                    NodeList NmElmntLstid = Elmnt.getElementsByTagName("Idcauhoi");
                    Element fstNmElmntid = (Element) NmElmntLstid.item(0);
                    NodeList fstNmid = fstNmElmntid.getChildNodes();
                    ch.setIdcauhoi(Integer.parseInt(fstNmid.item(0).getNodeValue()));



                    NodeList NmElmntLst = Elmnt.getElementsByTagName("Noidung");
                    Element fstNmElmnt = (Element) NmElmntLst.item(0);
                    NodeList fstNm = fstNmElmnt.getChildNodes();
                    ch.setQuestion(fstNm.item(0).getNodeValue());





                    //System.out.println("First Name : "  + ((Node) fstNm.item(0)).getNodeValue());            
                    NodeList lstNmElmntLst = Elmnt.getElementsByTagName("Luachon");

                    for (int i = 0; i < 4; i++) {
                        Element lstNmElmnt = (Element) lstNmElmntLst.item(i);
                        NodeList lstNm = lstNmElmnt.getChildNodes();
                        //System.out.println("Last Name : " + ((Node) lstNm.item(0)).getNodeValue());
                        switch (i) {
                            case 0:
                                ch.setAnswer_1(lstNm.item(0).getNodeValue());
                                break;
                            case 1:
                                ch.setAnswer_2(lstNm.item(0).getNodeValue());
                                break;
                            case 2:
                                ch.setAnswer_3(lstNm.item(0).getNodeValue());
                                break;
                            case 3:
                                ch.setAnswer_4(lstNm.item(0).getNodeValue());
                                break;
                        }
                    }
                    NodeList dapan = Elmnt.getElementsByTagName("Dapan");
                    Element elmdapan = (Element) dapan.item(0);
                    NodeList nldapan = elmdapan.getChildNodes();
                    //System.out.println("Dáp án : "  + ((Node) nldapan.item(0)).getNodeValue()); 
                    ch.setResult(Integer.parseInt(nldapan.item(0).getNodeValue()));

                    NodeList nstLevel = Elmnt.getElementsByTagName("Dokho");
                    Element elmlevel = (Element) nstLevel.item(0);
                    NodeList lstLevel = elmlevel.getChildNodes();
                    //System.out.println("độ khó : "  + ((Node) lstLevel.item(0)).getNodeValue()); 
                    if (Integer.parseInt(lstLevel.item(0).getNodeValue()) == 1) {
                        listQuestionEasy.add(ch);
                    } else if (Integer.parseInt(lstLevel.item(0).getNodeValue()) == 2) {
                        listQuestionNormal.add(ch);
                    } else {
                        listQuestionHard.add(ch);
                    }
                    ch.setLevel(Integer.parseInt(lstLevel.item(0).getNodeValue()));
                    dsch.add(ch);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        setFlag(1);
        return dsch;
    }
    // input : 2,3,4,5,6
    // output : array list CauHoi[CauHoi 1, CauHoi 2,...,CauHoi n]
    public static ArrayList getQuestionFromWarehouse(String s) {
        ArrayList newArr = new ArrayList();
        String[] str = s.split(",");
        for(int i = 0; i < str.length; i++)
        {
            CauHoi ch = timCauHoi(Integer.parseInt(str[i]), dsch);
            newArr.add(ch);
        }
        return newArr;
    }
    // arr : mảng các câu hỏi dễ, khó hoặc trung bình
    // b : 20
    // max : 25
    // Nghĩa là random 20 lần nhưng nằm trong khoảng (0, 25)
    public static ArrayList randomQuestion(int a, int b, int max, ArrayList arr) {
        Random random = new Random();
        ArrayList newArr = new ArrayList();
        Hashtable h = new Hashtable();
        for(int i = a; i < b; i++)
        {
            while (true) {
                int result = random.nextInt(max);
                if (result >= a && !h.containsKey(result)) {
                    h.put(result, result);
                    newArr.add(arr.get(result));
                    break;
                }
            }
        }
        return newArr;
    }
    public static String SplitQuestions(ArrayList arr) {
        String str = "";
        for(int i = 0; i < arr.size(); i++)
        {
            CauHoi ch = new CauHoi();
            ch = (CauHoi)arr.get(i);
            str = str + ch.getIdcauhoi();
            if(i != arr.size() - 1)
                str = str + ",";
        }
        return str;
    }
    // Input : id câu hỏi
    // Output : id câu hỏi - đáp án
    public static Hashtable getAnswer(String s) {
        Hashtable mappingAnswer = new Hashtable();
        String[] str = s.split(",");
        for(int i = 0; i < str.length; i++)
        {
            CauHoi ch = timCauHoi(Integer.parseInt(str[i]), dsch);
            mappingAnswer.put(str[i], ch.getResult());
        }
        return mappingAnswer;
    }
    public static int countAnswerRight(Hashtable mappingAnswer, String question, String answer) {
        String[] strQuestion = question.split(",");
        String[] strAnswer = answer.split(",");
        int result = 0;
        for(int i = 0; i < strQuestion.length; i++)
        {
            int value = (Integer)mappingAnswer.get((strQuestion[i].toString()));
            if(value == Integer.parseInt(strAnswer[i].toString()))
                result = result + 1;
        }
        return result;
    }
    public static CauHoi timCauHoi(int maCauHoi, ArrayList arr) {
        CauHoi ch = new CauHoi();
        for (int i = 0; i < arr.size(); i++) {
            ch = (CauHoi) arr.get(i);
            if (ch.getIdcauhoi() == maCauHoi) {
                return ch;
            }
        }
        return ch;
    }

    public static void danhSachCauHoiCuaDe(String strDe, ArrayList nganHangCauHoi, ArrayList dsCauHoiCuaDe) {
        String[] split;
        split = strDe.split(" ");
        for (int i = 0; i < split.length; i++) {
            int idcauhoi = Integer.parseInt(split[i]);
            CauHoi ch = NganHangCauHoi.timCauHoi(idcauhoi, nganHangCauHoi);
            dsCauHoiCuaDe.add(ch);

        }
    }

    /**
     * @return the listQuestionEasy
     */
    public static ArrayList getListQuestionEasy() {
        return listQuestionEasy;
    }

    /**
     * @param aListQuestionEasy the listQuestionEasy to set
     */
    public static void setListQuestionEasy(ArrayList aListQuestionEasy) {
        listQuestionEasy = aListQuestionEasy;
    }

    /**
     * @return the listQuestionNormal
     */
    public static ArrayList getListQuestionNormal() {
        return listQuestionNormal;
    }

    /**
     * @param aListQuestionNormal the listQuestionNormal to set
     */
    public static void setListQuestionNormal(ArrayList aListQuestionNormal) {
        listQuestionNormal = aListQuestionNormal;
    }

    /**
     * @return the listQuestionHard
     */
    public static ArrayList getListQuestionHard() {
        return listQuestionHard;
    }

    /**
     * @param aListQuestionHard the listQuestionHard to set
     */
    public static void setListQuestionHard(ArrayList aListQuestionHard) {
        listQuestionHard = aListQuestionHard;
    }

    /**
     * @return the flag
     */
    public static int getFlag() {
        return flag;
    }

    /**
     * @param aFlag the flag to set
     */
    public static void setFlag(int aFlag) {
        flag = aFlag;
    }
}
